package com.steven.forum.controller;

import com.alibaba.fastjson.JSONObject;
import com.steven.forum.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
public class ForumController {

    @Autowired
    private ForumService service;

    @PostMapping("/forum/list/hot")
    public JSONObject queryHotForumList(@RequestHeader Long uid, Long page) {
        return service.queryHotForumList(uid, page);
    }

    @PostMapping("/forum/list/new")
    public JSONObject queryNewForumList(@RequestHeader Long uid, Long page) {
        return service.queryNewForumList(uid, page);
    }

    @PostMapping("/forum/query")
    public JSONObject queryForumByFid(@RequestHeader Long uid, Long fid) {
        return service.queryForumByFid(uid, fid);
    }

    @PostMapping("/forum/add")
    public JSONObject addForum(@RequestHeader Long uid, String title, String content, MultipartFile[] images, HttpServletResponse response) {
        return service.addForum(uid, title, content, images, response);
    }

    @PostMapping("/forum/addHistory")
    public JSONObject addUserForumHistory(@RequestHeader Long uid, Long fid, HttpServletResponse response) throws IOException {
        if (uid == null || fid == null) {
            response.sendError(500, "缺失请求参数");
        }
        return service.addUserHistory(uid, fid);
    }

    @PostMapping("/forum/discuss/list")
    public JSONObject queryDiscuss(Long forumId) {
        return service.queryDiscuss(forumId);
    }

    @PostMapping("/forum/discuss/add")
    public JSONObject addDiscuss(@RequestHeader Long uid, Long fid, String content) {
        return service.addDiscuss(uid, fid, content);
    }

    @PostMapping("/complain/add")
    public JSONObject addComplain(@RequestHeader Long uid, Long targetUid, String content, MultipartFile[] images, HttpServletResponse response) throws IOException {
        if (uid==null||uid==0){
            response.sendError(500,"传参错误");
            return null;
        }
        return service.addComplain(uid, targetUid, content, images, response);
    }

    @PostMapping("/complain/query")
    public JSONObject queryComplain(@RequestHeader Long uid, Long lastCreateTime, HttpServletResponse response) {
        return service.queryComplain(uid, lastCreateTime);
    }

    @PostMapping("/complain/update")
    public JSONObject updateComplain(@RequestHeader Long uid, Long id, HttpServletResponse response) {
        return service.updateComplain(uid, id);
    }
}
